import axios from 'axios';

// 设置默认配置
axios.defaults.baseURL = 'http://47.94.46.113:7788';

// 拦截器
axios.interceptors.request.use(config => {
  // Do something before request is sent
  let token = sessionStorage.getItem('token')
  if (config.path != '/user/login') {
    config.headers['Authorization'] = token
  }
  return config;
}, error => {
  // Do something with request error
  return Promise.reject(error);
});

axios.interceptors.response.use(response => {
  // Do something before response is sent
  response = {
    ...response,
    data: response.data.data,
    statusText: response.data.message,
    timestamp: response.data.timestamp
  }
  return response;
}, error => {
  // Do something with response error
  return Promise.reject(error);
});

// 导出方法
export function post_json (url, data) {
  return axios({
    url,
    method: 'post',
    data,
    timeout: 10000
  })
}

export function get (url, params) {
  return axios({
    url,
    method: 'get',
    params,
    timeout: 10000
  })
}